General Tips on Programming תוכנה 1 סמסטר א' תשע"א תרגול מס' 6 מנשקים, דיאגרמות וביטים * רובי בוים ומתי שמרת Write your code modularly top-down approach Compile + test functionality on the fly Start with an empty program/classes Add content gradually and keep testing If something goes wrong, probably the bug is in the latest change * לא בהכרח בסדר הזה General Tips on Programming Even More Tips Use the Debugger to follow your execution flow and find what went wrong Understanding is good but not enough you must practice! Based on a true story: Do not send us code Do not send us emails saying the code you gave us does not work, before you make sure the original given code is problematic When you ask questions be as specific as you can, give as much information about the problem and your trials as possible Even More Tips Late submissions / appeals on homework grades? Please contact the graders odedelba@post.tau.ac.il המערכת הבנקאית נתאר את מערכת התוכנה שלנו בעזרת דיאגרמות דיאגרמות סטטיות: תיאור היחסים בין המחלקות השונות במערכת דיאגרמות דינאמיות: תיאור ההתנהגות של המערכת בזמן ריצה מצב האוביקטים תיאור של תרחיש 6 1
Class Diagram המחלקה Customer Customer Account public Account(Customer customer, long id) public withdraw(double amount) public (double amount) public transferto(double amount, Account otheraccount) public double getbalance() public Customer getowner() public double getnumber() Aggregation (has-a) public Customer(String name, String id) * public String getname() public String getid() main Association 7 public class Customer public Customer(String name, String id) this.name = name; this.id = id; public String getname() return name; public String getid() return id; private String name; private String id; 8 Toy Program public class public static main(string[] args) 9 10 main ( ) Toy Program public class public static main(string[] args) "024847638" 11 12 main ( ) customer2: "Rita Stein" 2
Toy Program public class public static main(string[] args) Account: number: 1234 Account: number: 2984 Account: number: 5678 "024847638" "Rita Stein" 13 14 main ( ) customer2: account1: account2: account3: Message Sequence Chart Message Sequence Chart public class public static main(string[] args) 15 16 time main 1000 100, account3 account1 account2 account3 balance: 900 number: 1234 500 transferto 300 balance: 200 number: 5678 withdraw 100 balance: 100 number: 2984 Output מנשקים public class public static main(string[] args) output: account1 has 900.0 account2 has 200.0 17 מנשק )interface( הוא מבנה תחבירי ב Java המאפשר לחסוך בקוד לקוח קוד אשר משתמש במנשק יוכל בזמן ריצה לעבוד עם מגוון מחלקות המממשות את המנשק הזה )ללא צורך בשכפול הקוד עבור כל מחלקה( דוגמא: נגן מוזיקה אשר מותאם לעבוד עם קובצי מוזיקה )mp3( ועם קובצי וידאו )mp4( 3
Playing Mp3 Playing VideoClips public class MP3Song public class Player public class VideoClip public class Player public play() private boolean repeat; public play() // same as before... // audio codec calculations, private boolean shuffle; // video codec calculations, // play the song... public playsongs(mp3song[] songs) do // play the clip... public playvideos(videoclip[] clips) do // does complicated stuff // does complicated stuff Collections.shuffle(Arrays.asList(clips)); // related to MP3 format... Collections.shuffle(Arrays.asList(songs)); // related to MP4 format... for (MP3Song song : songs) for (VideoClip videoclip : clips) videoclip.play(); song.play(); while (repeat); while (repeat); שימוש במנשק שכפול קוד public playsongs(mp3song[] songs) do Collections.shuffle(Arrays.asList(songs)); for (MP3Song song : songs) song.play(); while (repeat); למרות ששני השרותים נקראים play() אלו פונקציות שונות! public play (Playable[] items) do Collections.shuffle(Arrays.asList(items)); for (Playable item : items) item.play(); public playvideos(videoclip[] clips) do Collections.shuffle(Arrays.asList(clips)); for (VideoClip videoclip : clips) videoclip.play(); while (repeat); public interface Playable public play(); while (repeat); נרצה למזג את שני קטעי הקוד מימוש המנשק ע"י הספקים public class VideoClip implements Playable @Override public play() // render video, play the clip on screen... // does complicated stuff related to video formats... public class MP3Song implements Playable @Override public play() // audio codec calculations, play the song... // does complicated stuff related to MP3 format... מערכים פולימורפים Playable[] playables = new Playable[3]; playables[0] = new MP3Song(); playables[1] = new VideoClip(); playables[2] = new MP4Song(); // new Playable class Player player = new Player(); // init player... public play (Playable [] items) player.play(playables); עבור כל איבר במערך יקרא ה play() המתאים do Collections.shuffle(Arrays.asList(items)); for (Playable item : items) item.play(); while (repeat); 4
פעולות על סיביות - דוגמאות פעולות על סיביות אופרטורים לביצוע פעולות על ביטים רק על טיפוסים איטנגרליים char( )int, short, byte, ~ << >> >>> & ^ Unary bitwise complement Signed left shift Signed right shift Unsigned right shift Bitwise AND Bitwise XOR Bitwise OR 25 3 ~3-3 3 << 2-3 >> 1-3 >>> 1 ייצוג בינארי 00000000000000000000000000000011 11111111111111111111111111111100 11111111111111111111111111111101 00000000000000000000000000001100 11111111111111111111111111111110 01111111111111111111111111111110 int 32 ביטים מה נקבל מ & 3?i שני הביטים הימניים של i ומה נקבל מ?i & 0xF0 26 5